package org.coco24.pmstudio.widget

import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.badlogic.gdx.utils.Array
import com.kotcrab.vis.ui.widget.VisSelectBox
import com.kotcrab.vis.ui.widget.VisTable
import com.kotcrab.vis.ui.widget.VisTextField

class EvolutionEntry(val selections: Array<Triple<String, String, Any>>): VisTable()
{
    val selectBox = VisSelectBox<String>()
    val textField = VisTextField()
    var childEntry: EvolutionEntry? = null
    val child = VisTable()
    val value: Any
    get()
    {
        if(childEntry == null)
        {
            return textField.text
        }
        else
        {
            return childEntry!!
        }
    }
    init
    {
        val selectionsString = Array<String>()
        selections.forEach {
            selectionsString.add(it.second)
        }
        selectBox.items = selectionsString
        selectBox.addListener(object : ChangeListener()
        {
            override fun changed(event: ChangeEvent?, actor: Actor?)
            {
                update()
            }

        })
        add(selectBox)
        add(child)
        pack()
        update()
    }
    fun update()
    {
        child.clear()
        val selected = selections[selectBox.selectedIndex].third
        if(selected is Array<*>)
        {
            val entry = EvolutionEntry(selected as Array<Triple<String, String, Any>>)
            childEntry = entry
            child.add(entry)
        }
        else if(selected is String)
        {
            childEntry = null
            child.add(textField)
        }
        child.pack()
    }
}